_Pragma("once");

#include <string>
#include <map>


#include "SocketCommunicator.h"
#include "UriInfo.h"
#include "Utility.h"

struct HttpHeader{
    std::string fileName;
    Utility::StringMap properties;
	ssize_t fileSize;
	ssize_t contentLength;
	std::string contentType;
	int statusCode;

	void reset(){
	    fileName.clear();
        properties.clear();
        fileSize = 0;
        contentLength = 0;
        contentType.clear();
        statusCode = 0;
	}
};


const std::string RANGE{"Range"};
const std::string IF_RANGE{"If-Range"};
const std::string CONTENT_RANGE{"Content-Range"};
const std::string ETAG{"ETag"};
const std::string PROPERTY_SPLITTER{": "};
const std::string PROPERTY_END{"\r\n"};

class HttpCommunicator{

public:
    HttpCommunicator(const UriInfo& uriInfo) : muriInfo(uriInfo){
    }

    HttpCommunicator(){
    }

    bool connect(const UriInfo& ipAddressInfo) noexcept {
        if (!(ipAddressInfo == muriInfo)){
            memset(&msockaddr, 0, sizeof(msockaddr));
        }
        muriInfo = ipAddressInfo;
        msocket.allocHandle();
        msocket.setTimeout({3,0});
        return msocket.connect(muriInfo, msockaddr);
    }


    ssize_t sendHeader(const Utility::StringMap& properties) ;

    ssize_t parseHeader(HttpHeader& header);


    UriInfo muriInfo;// http://192.168.1.100:8081/pavt/5.DOWNLOAD/xiao/update.tar.gz
    sockaddr_in msockaddr;
    SocketCommunicator msocket;
};





